﻿using System;
using System.Data.Entity.Infrastructure;
using ScrumPilot.Domain.Repositories;

namespace ScrumPilot.Domain
{
    public class UnitOfWork : IDisposable
    {
        private readonly DBScrumPilotEntities _context;
        public UnitOfWork()
        {
            _context = new DBScrumPilotEntities();
        }

        #region Repository
        private ProjectRepository _projectRepository;
        public ProjectRepository ProjectRepository
        {
            get
            {
                if (this._projectRepository == null)
                    this._projectRepository = new ProjectRepository(_context);
                return _projectRepository;
            }
        }

        private TfsServerParamsRepository _tfsServerParamsRepository;
        public TfsServerParamsRepository TfsServerParamsRepository
        {
            get
            {
                if (this._tfsServerParamsRepository == null)
                    this._tfsServerParamsRepository = new TfsServerParamsRepository(_context);
                return _tfsServerParamsRepository;
            }
        }

        private ScrumPilotParamRepository _scrumPilotParamRepository;
        public ScrumPilotParamRepository ScrumPilotParamRepository
        {
            get
            {
                if (this._scrumPilotParamRepository == null)
                    this._scrumPilotParamRepository = new ScrumPilotParamRepository(_context);
                return _scrumPilotParamRepository;
            }
        }

        private UserRepositery _userRepositery;
        public UserRepositery UserRepositery
        {
            get
            {
                if (this._userRepositery == null)
                    this._userRepositery = new UserRepositery(_context);
                return _userRepositery;
            }
        }

        private SprintRepository _sprintRepositery;
        public SprintRepository SprintRepository
        {
            get
            {
                if (this._sprintRepositery == null)
                    this._sprintRepositery = new SprintRepository(_context);
                return _sprintRepositery;
            }
        }

        private SprintProjectIterationRepository _sprintProjectIterationRepository;
        public SprintProjectIterationRepository SprintProjectIterationRepository
        {
            get
            {
                if (this._sprintProjectIterationRepository == null)
                    this._sprintProjectIterationRepository = new SprintProjectIterationRepository(_context);
                return _sprintProjectIterationRepository;
            }
        }
        private EventRepository _eventRepository;

        public EventRepository EventRepository
        {
            get
            {
                if (this._eventRepository == null)
                    this._eventRepository = new EventRepository(_context);
                return _eventRepository;
            }
        }

        private KanbanIterationRepository _kanbanIterationRepository;

        public KanbanIterationRepository KanbanIterationRepository
        {
            get
            {
                if (this._kanbanIterationRepository == null)
                    this._kanbanIterationRepository = new KanbanIterationRepository(_context);
                return _kanbanIterationRepository;
            }
        }

        private FollowedUserRepositery _followedUserRepositery;
        public FollowedUserRepositery FollowedUserRepositery
        {
            get
            {
                if (this._followedUserRepositery == null)
                    this._followedUserRepositery = new FollowedUserRepositery(_context);
                return _followedUserRepositery;
            }
        }
        #endregion

        public int Save()
        {
            try
            {
                return _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return -1;
            }
            catch (Exception)
            {
                return -2;
            }

        }

        private bool _disposed;

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            this._disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
